<?php
namespace app\index\controller;

use think\Controller;
use app\index\validate\Doregister;
use app\index\model\User;

class Register extends controller
{
    public function register()
    {
        $inputText = file_get_contents("php://input");
        $inputData = json_decode($inputText,true);

        $userId = $inputData["useId"];
        $username = $inputData["userName"];
        $password = $inputData["password"];
        $isteacher = $inputData["isteacher"];

        // $retData = [
        //     "useId"=>$useId,
        //     "username" => $username,
        //     "password" => $password,
        //     "isteacher" => $isteacher
        // ];

        $validate = new Doregister();
        if(!$validate->check($inputData)){
            $hint=$validate->getError();
            $retData = [
                "status"=>"fail",
                "message" => $hint
            ];
        }else{
            if($isteacher===true){
                $userstatus = 1;
            }else{
                $userstatus = 0;
            }
            $UserModel = new User();
            $user = $UserModel->addUser($userId,$username,$password,$userstatus);
            if($user === true){
                $hint = $user;
                $retData = [
                    "status"=>"fail",
                    "message" => $hint
                ];
            }else{
                $retData = [
                    "status"=>"success",
                    "message" =>"success"
                ];
            }
        }

        die(json_encode($retData));
        return $retData;
    }
}